﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Srvtools;

namespace EEPManager
{
    public partial class frmDDSelTab : Form
    {
        //public List<string> SelTabList = new List<string>();
        public ArrayList SelTabList = new ArrayList();
        public frmDDSelTab(ArrayList SelectedTabList)
        {
            InitializeComponent();
            SelTabList = SelectedTabList;
        }

        private void frmDDSelTab_Load(object sender, EventArgs e)
        {
            LoadItem();
        }

        private bool bHasLoaded = false;
        private void clstTableName_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (bHasLoaded)
            {
                if (this.clstTableName.SelectedItem != null)
                {
                    if (e.NewValue == CheckState.Checked)
                    {
                        this.SelTabList.Add(this.clstTableName.SelectedItem.ToString());
                    }
                    else
                    {
                        this.SelTabList.Remove(this.clstTableName.SelectedItem.ToString());
                    }
                }
            }
        }

        private void LoadItem()
        {
            this.clstTableName.Items.Clear();
            foreach (DataRow row in this.infoDsDBTables.RealDataSet.Tables[0].Rows)
            {
                this.clstTableName.Items.Add(row[0].ToString());
            }
            int i = SelTabList.Count;
            for (int j = 0; j < i; j++)
            {
                if (this.clstTableName.Items.Contains(SelTabList[j]))
                {
                    int m = this.clstTableName.Items.IndexOf(SelTabList[j]);
                    this.clstTableName.SetItemChecked(m, true);
                }
            }
            bHasLoaded = true;    
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String where = tbQuery.Text == null ? String.Empty : tbQuery.Text;
            where = where.Replace("'", "''");

            String type = "";
            object[] myRet = CliUtils.CallMethod("GLModule", "GetDataBaseType", new object[] { CliUtils.fLoginDB });
            if (myRet != null && myRet[0].ToString() == "0")
                type = myRet[1].ToString();
            switch (type)
            {
                case "1": infoDsDBTables.SetWhere("name like '%" + where + "%'"); break;
                case "2": infoDsDBTables.SetWhere("name like '%" + where + "%'"); break;
                case "3": infoDsDBTables.SetWhere("OBJECT_NAME like '%" + where + "%'"); break;
                case "4": infoDsDBTables.SetWhere("TABNAME like '%" + where + "%'"); break;
                case "5": infoDsDBTables.SetWhere("name like '%" + where + "%'"); break;
            }
            LoadItem();
        }
    }
}